package les

class ReciboController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [reciboInstanceList: Recibo.list(params), reciboInstanceTotal: Recibo.count()]
    }

    def create = {
        def reciboInstance = new Recibo()
		reciboInstance.properties = params
        return [reciboInstance: reciboInstance]
    }

    def save = {
        def reciboInstance = new Recibo(params)
		
		if(params.valorRecebido.toFloat() < 0){
			flash.message = "O valor do campo Valor Recebido deve ser maior que zero."
			render(view: "create")
			return;
		}
		
		if (reciboInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'recibo.label', default: 'Recibo'), reciboInstance.id])}"
            redirect(action: "show", id: reciboInstance.id)
        }
        else {
            render(view: "create", model: [reciboInstance: reciboInstance])
        }
    }

    def show = {
        def reciboInstance = Recibo.get(params.id)
        if (!reciboInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'recibo.label', default: 'Recibo'), params.id])}"
            redirect(action: "list")
        }
        else {
			session.recibo = reciboInstance
            [reciboInstance: reciboInstance]
        }
    }

    def edit = {
        def reciboInstance = Recibo.get(params.id)
        if (!reciboInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'recibo.label', default: 'Recibo'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [reciboInstance: reciboInstance]
        }
    }

    def update = {
        def reciboInstance = Recibo.get(params.id)
        if (reciboInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (reciboInstance.version > version) {
                    
                    reciboInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'recibo.label', default: 'Recibo')] as Object[], "Another user has updated this Recibo while you were editing")
                    render(view: "edit", model: [reciboInstance: reciboInstance])
                    return
                }
            }
            reciboInstance.properties = params
            if (!reciboInstance.hasErrors() && reciboInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'recibo.label', default: 'Recibo'), reciboInstance.id])}"
                redirect(action: "show", id: reciboInstance.id)
            }
            else {
                render(view: "edit", model: [reciboInstance: reciboInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'recibo.label', default: 'Recibo'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def reciboInstance = Recibo.get(params.id)
        if (reciboInstance) {
            try {
                reciboInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'recibo.label', default: 'Recibo'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'recibo.label', default: 'Recibo'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'recibo.label', default: 'Recibo'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def gerarRecibo = {
		if (!session.usuarioLogado) {
			redirect(uri: '/usuario/login.gsp')
			return
		}
		
		String valor = session.recibo.valorRecebido
		
		String valorArmazenar = ""
		for (int i = 0; i <= valor.length() - 1; i++){
			if(valor.charAt(i) == '.'){
				valorArmazenar = valorArmazenar + ","
			} else{
				valorArmazenar = valorArmazenar +  valor.charAt(i).toString();
			}
		}
		
		int ultimoIndice = valorArmazenar.length() - 1
		int ultimaVirgula = valorArmazenar.lastIndexOf(',', valorArmazenar.length() - 1)
			
				
		if((ultimoIndice - ultimaVirgula) == 1){
			
			valorArmazenar = valorArmazenar + "0"
		}
		
		session.valorImprimir = valorArmazenar
		
	}
}
